﻿using System.Modules.ExceptionHanlding;
using System.Modules.Extensions;
using Microsoft.SharePoint;

namespace System.Modules.SharePoint.Extensions
{
    /// <summary>
    /// SP Folder Collection Extensions
    /// </summary>
    public static class SPFolderCollectionExtensions
    {
        #region Try Get Folder
        /// <summary>
        /// This Method tries getting a folder from name. 
        /// If Folder does not exists, it will returns null.
        /// </summary>
        /// <param name="instance"></param>
        /// <param name="folderName"></param>
        /// <returns>
        /// SPFolder
        /// </returns>
        public static SPFolder TryGetFolder(this SPFolderCollection instance, string folderName)
        {
            SPFolder folder;
            try
            {
                folder = instance[folderName];

                if (!folder.Exists)
                {
                    folder = null;
                }
            }
            catch (ArgumentException)
            {
                folder = null;
            }
            return folder;
        }
        #endregion

        #region Ensure Folder
        /// <summary>
        /// This Method ensures folder. If the folder does not exist, it will eb created.
        /// </summary>
        /// <param name="instance"></param>
        /// <param name="folderName"></param>
        /// <returns>
        /// SPFolder
        /// </returns>
        public static SPFolder EnsureFolder(this SPFolderCollection instance, string folderName)
        {
            string legalName = folderName.RemoveIllegalFileNameCharachters();
            SPFolder folder = (instance.TryGetFolder(folderName) ?? instance.TryGetFolder(legalName)) ?? instance.Add(legalName);
            if (folder != null)
            {
                SPListItem folderItem = folder.Item;
                if (folderItem != null)
                {
                    if (folderItem.ModerationInformation != null)
                    {
                        folderItem.ModerationInformation.Status = SPModerationStatusType.Approved;
                    }
                    try
                    {
                        folderItem.Update();
                    }
                    catch (Exception exception)
                    {
                        exception.Handle();
                    }
                }
            }
            return folder;
        }
        #endregion

        #region Ensure New Folder
        /// <summary>
        /// This method ensures a new folder. If the folder exists, it will be deleted, and a new folder will be created.
        /// </summary>
        /// <param name="instance"></param>
        /// <param name="folderName"></param>
        /// <returns>
        /// SPFolder
        /// </returns>
        public static SPFolder EnsureNewFolder(this SPFolderCollection instance, string folderName)
        {
            string legalName = folderName.RemoveIllegalFileNameCharachters();
            SPFolder folder = instance.TryGetFolder(folderName) ?? instance.TryGetFolder(legalName);
            if (folder != null)
            {
                folder.Delete();
                folder = instance.Add(legalName);
            }
            if (folder != null)
            {
                SPListItem folderItem = folder.Item;
                if (folderItem != null)
                {
                    if (folderItem.ModerationInformation != null)
                    {
                        folderItem.ModerationInformation.Status = SPModerationStatusType.Approved;
                    }
                    try
                    {
                        folderItem.Update();
                    }
                    catch (Exception exception)
                    {
                        exception.Handle();
                    }
                }
            }
            return folder;
        }
        #endregion
    }
}